<template>
  <div class="model-form">
    <el-page-header @back="goBack" content="增加商家">
    </el-page-header>
    <div class="model-content">
      <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="门头" size="mini" prop="logo">
              <avatar v-model="form.logo"></avatar>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="商家名称" size="mini" prop="name">
              <el-input v-model="form.name" name="name">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="所属分类" size="mini" prop="shopCatalog">
              <el-select style="width: 100%;" v-model="form.storeCatalog" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in shopCatalogOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" size="mini" prop="address">
              <el-input v-model="form.address" name="address">
                <template #append>
                  <el-button type="primary" size="small" @click="geo">定位</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地图信息" size="mini">
              <qqmap zoom="18" @poi-change="changePoi" :lat="form.lat" :lng="form.lng"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" size="mini" prop="contact">
              <el-input v-model="form.contact" name="contact">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="客服电话" size="mini" prop="tel">
              <el-input v-model="form.phone" name="tel">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="返现折扣" size="mini" prop="commissionRate">
              <el-input v-model="form.rebate" name="commissionRate">
                <template #append>
                  %
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会员福利" size="mini" prop="note">
              <el-input v-model="form.privilege" name="note" type="textarea">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商家详情" size="mini" prop="note">
              <el-input v-model="form.note" name="note" type="textarea">
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24" style="display: none">


            <el-form-item label="商家视频" size="mini" style="display: flex">

              <el-upload
                  :accept="'image/*,video/*'"
                  class="avatar-uploader"
                  action="https://model.nbsaas.com/storage/upload.htm"
                  :show-file-list="false"
                  :on-success="uploadVideoSuccess"
                  :before-upload="uploadVideoBefore"
              >
                <div style="height: 100px;width: 200px;" v-if="form.videoPath">
                  <sc-video :src="form.videoPath"></sc-video>
                </div>
                <el-icon class="avatar-uploader-icon" size="30"  style="margin-left: 20px;" >
                  <Plus/>
                </el-icon>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="23" style="display: none">
            <el-form-item label="商家环境" size="mini" prop="couponFee">
              <el-upload
                  v-model:file-list="form.images"
                  action="https://cloud.newbyte.ltd/storage/upload.htm"
                  list-type="picture-card"
                  :on-remove="handleRemove"
                  :on-success="uploadSuccess"
                  :on-preview="handlePictureCardPreview"
                  :before-remove="beforeRemove"
              >
                <el-icon>
                  <Plus/>
                </el-icon>
              </el-upload>

              <el-dialog v-model="dialogVisible">
                <img w-full :src="dialogImageUrl" alt="Preview Image" style="width: 99%;height: auto; "/>
              </el-dialog>
            </el-form-item>

          </el-col>
          <el-col :span="22">
            <el-form-item>
              <el-button @click="goBack">取消</el-button>
              <el-button type="primary" @click="add">确定</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import common from "@/mixins/common.js";

import avatar from "@/components/avatar.vue";
import qqmap from "@/components/qqmap.vue";
import scVideo from "@/components/scVideo/index.vue";

export default {
  name: "Shop_add",
  mixins: [common],
  components: {scVideo, avatar,qqmap},
  data() {
    return {
      form: {
        logo: '',
        name: '',
        storeCatalog: '',
        sortNum: '',
        address: '',
        parent: '',
        contact: '',
        tel: '',
        fee: '',
        couponName: '',
        couponFee: '',
        chains:[]
      },
      chains:[],
      shopCatalogOptions: [],
      rules: {
        name: [
          {required: true, message: '请输入商家名称', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {

    async geo() {
      let param = {};
      param.address = this.form.address;
      let self = this;
      let res = await this.$http.form("/area/lbs", param);
      if (res.code === 200) {
        let result=res.data;
        self.form.lat = result.lat;
        self.form.lng = result.lng;
        if (result.provinceId) {
          self.form.province = result.provinceId;
        }
        if (result.cityId) {
          self.form.city = result.cityId;
        }
        if (result.countyId) {
          self.form.county = result.countyId;
        }
      }
    },
    changePoi(event) {
      console.log(event);
      this.form.lat = event.lat;
      this.form.lng = event.lng;
    },
    async add() {
      try {
        let valid = await this.$refs["ruleForm"].validate();
        if (!valid) {
          return;
        }
      } catch (e) {
        return;
      }

      await this.addData();
    },
    async addData() {
      let data = this.form;
      let res = await this.$http.form("/store/create", data);
      if (res.code !== 200) {
        this.$message.error(res.msg);
      }

      this.$message({
        message: '添加数据成功',
        type: 'success'
      });
      this.$router.go(-1);
    },
    async loadShopCatalogOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      let res = await this.$http.form("/storeCatalog/list", param);
      if (res.code === 200) {
        self.shopCatalogOptions = res.data;
      }
    },
    async loadChainShops() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      param.shopType="chain";
      let res = await this.$http.form("/shop/list", param);
      if (res.code === 200) {
        self.chains = res.data;
      }
    }
  },
  mounted() {
    this.loadShopCatalogOptions();
  }
}

</script>

<style scoped>
</style>
